package com.caikun.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.caikun.entity.Userinfos;
import com.caikun.services.UserinfosService;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class LoginController
 */
public class LoginController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	 String bind =request.getParameter("bind");
	 String password=request.getParameter("password");
	 PrintWriter out = response.getWriter();
	 		if(bind==null||bind==""||password==null||password==""){
	 				out.write("{\"status\":\"444\"}");
	 		}else{
	 			UserinfosService us = new UserinfosService();
	 			ObjectMapper mapper= new ObjectMapper();
	 			List l = us.findLogin(bind, password);
	 		if(l.isEmpty()){
	 			out.write("{\"status\":\"444\"}");
	 		}else{
	 			out.write("{\"status\":\"666\",\"bind\":"+bind+"}");
	 		}
	 		request.getSession().setAttribute("bind",bind);
//				System.out.println(bind);
//				System.out.println(us.findLogin(bind, password));
	 		}
	 		out.flush();
	 		out.close();
		}
}





